<?

$_values = array('id', 'id_index', 'attr', 'assoc', 'name', 'value', 'text', 'tag');

if (($c = $DATA['component']) && $c['id']) {

	$__settings = datafetch_array('db_list', array('table' => tb('data'), 
		'where' => "id_parent = $c[id] AND id_data=".D_MB_SETTINGS, 'index' => 'name'));

	$__path = FP_UPLOAD_PATH.$__settings['path']['value'].'/';

	$__data = datafetch_array('db_list', array('table' => tb('data'), 
		'where' => "id_parent = $c[id] AND id_data=".D_MB_DATA, 'index' => 'id'));
		
	$__meta = datafetch_array('db_list', array('table' => tb('data'), 
		'where' => "id_parent in (".implode(',', array_keys($__data)).") AND id_data=".D_MB_META, 'index' => 'id'));

	foreach($__meta as $id => $v)
		if (isset($__data[$v['id_parent']]))
			$__data[$v['id_parent']]['meta'][] = $v;



	$e['root']->appendChild($s = $e['document']->createElement('settings'));
	
	foreach($__settings as $id => $v) {
		$s->appendChild($x = $e['document']->createElement('set'));
		foreach($_values as $key)
			$x->setAttribute($key, $v[$key]);
	}

	$e['root']->appendChild($s = $e['document']->createElement('content'));

	foreach($__data as $id => $v) {
		$s->appendChild($x = $e['document']->createElement('data'));
		foreach($_values as $key)
			$x->setAttribute($key, $v[$key]);
			
		if (isset($v['meta']))
			foreach($v['meta'] as $meta) {
				$x->appendChild($m = $e['document']->createElement('meta'));
				foreach($_values as $key)
					$m->setAttribute($key, $meta[$key]);
			}

		if ($im = isImage($__path, $id)) {
			
			$fn = parse_fname($im);
			$x->setAttribute("image", '<id>.'.$fn['ext']);

			$e['attach'][] = array('archName' => $im, 'fName' => $_SERVER['DOCUMENT_ROOT']."$__path$im");
		}
			
	}

	return false;
} else exit;





$c = meta('catalog');

$_table	= 'item';			// where info is stored
$_order	= 'id_index';		// ordered by this field
$_crit	= "id_parent=$c";	// data selection criterium
$_path	= FP_UPLOAD_CAT;
$_tn		= FP_TN_DIR;
$_values	= array('title', 'text', 'article', 'price', 'price_old', 'nick', 'slogan', 'element', 'id_brand', 'description', 'descr_mat');	// which fields are stored
$_cattype = "catalog";

if (!is_dir($_SERVER['DOCUMENT_ROOT'].$_path)) mkdir($_SERVER['DOCUMENT_ROOT'].$_path);
if (!is_dir($_SERVER['DOCUMENT_ROOT'].FP_UPLOAD_PRICE)) mkdir($_SERVER['DOCUMENT_ROOT'].FP_UPLOAD_PRICE);

$DATA['objects'] = array_mark_bounds( datafetch_array('db_list', array('table' => tb($_table), 'where' => $_crit, 'order' => $_order, 'index' => 'id' )));

$DATA['sections'] = datafetch_array('db_list', array('table' => tb('struct, data'), 
	'where' => "data.id_parent = struct.id AND struct.template = 'main-catalog' AND data.id_data=1 AND data.name = 'catalog'", 
	'index' => 'id', 
	'select' => "struct.id AS id, struct.name AS name, data.value AS value"));



// tasks handling

if ($a = (int)param('delete')) {
	
	dataset('db_delete', tb($_table), "$_crit AND (id=$a)");

	dataset('db_delete', tb('data'), "id_data=".D_MATERIALS." AND id_parent=$a");

	@unlink($_SERVER['DOCUMENT_ROOT']."$_path$a.jpg");
	@unlink($_SERVER['DOCUMENT_ROOT']."$_path$a-2.jpg");
	@unlink($_SERVER['DOCUMENT_ROOT']."$_path$a-small.jpg");
	@unlink($_SERVER['DOCUMENT_ROOT']."$_path$a-elements.jpg");
	@unlink($_SERVER['DOCUMENT_ROOT']."$_path$a-special.jpg");
	@unlink($_SERVER['DOCUMENT_ROOT']."$_path$a-price.pdf");

	killdir($_SERVER['DOCUMENT_ROOT']."$_path$a/$_tn");
	killdir($_SERVER['DOCUMENT_ROOT']."$_path$a");

	killdir($_SERVER['DOCUMENT_ROOT']."$_path$a-2/$_tn");
	killdir($_SERVER['DOCUMENT_ROOT']."$_path$a-2");


//	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'delete')));
	exit;
}

if ($a = (int)param('show')) {
	
	dataset('db_update', tb($_table), "attr = attr | ".AA_VISIBLE, "$_crit AND (id=$a)");

//	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'show')));
	exit;
}

if ($a = (int)param('hide')) {
	
	dataset('db_update', tb($_table), "attr = attr & ~".AA_VISIBLE, "$_crit AND (id=$a)");

//	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'hide')));
	exit;
}

if ($a = (int)param('hot')) {
	
	dataset('db_update', tb($_table), "attr = attr | ".AA_HOT, "$_crit AND (id=$a)");

	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'hot')));
	exit;
}

if ($a = (int)param('cold')) {
	
	dataset('db_update', tb($_table), "attr = attr & ~".AA_HOT, "$_crit AND (id=$a)");

	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'cold')));
	exit;
}

if ($a = (int)param('vip')) {
	
	dataset('db_update', tb($_table), "attr = attr | ".AA_VIP, "$_crit AND (id=$a)");

	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'vip')));
	exit;
}

if ($a = (int)param('unvip')) {
	
	dataset('db_update', tb($_table), "attr = attr & ~".AA_VIP, "$_crit AND (id=$a)");

	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'unvip')));
	exit;
}

if ($a = (int)param('new')) {
	
	dataset('db_update', tb($_table), "attr = attr | ".AA_NEW, "$_crit AND (id=$a)");

	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'new')));
	exit;
}

if ($a = (int)param('unnew')) {
	
	dataset('db_update', tb($_table), "attr = attr & ~".AA_NEW, "$_crit AND (id=$a)");

	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'unnew')));
	exit;
}

if ($a = (int)param('on36')) {
	
	dataset('db_update', tb($_table), "attr = attr | ".AA_36, "$_crit AND (id=$a)");

	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'on36')));
	exit;
}

if ($a = (int)param('off36')) {
	
	dataset('db_update', tb($_table), "attr = attr & ~".AA_36, "$_crit AND (id=$a)");

	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'off36')));
	exit;
}


function sortByTitle($a, $b) {
	if (trim($a['title']) == trim($b['title'])) return 0;
	return (trim($a['title']) < trim($b['title'])) ? -1 : 1;
}

if ($a = strtolower(trim(param('sort')))) {
	
	switch($a) {
	
		case 'title':
	
			uasort($DATA['objects'], 'sortByTitle');
		
			$t = 1;
			foreach($DATA['objects'] as $id => $v) 
				dataset('db_update', tb($_table), "$_order = ".$t++, "$_crit AND id=$id");
			
			break;

		case 'manual':	// ajax request, no data should be returned

			$t = 1;
			
			parse_str(postdata('order'), $temp);
			
			if (is_array($temp = $temp['objects']) && sizeof($temp)) {

				foreach($temp as $id) {
					if ($id = (int)$id) {
						if ($DATA['objects'][$id]) {
							dataset('db_update', tb($_table), "$_order = ".$t++, "$_crit AND id=$id");
							unset($DATA['objects'][$id]);
						}
					}
				}
				
				if (sizeof($DATA['objects']))
					foreach($DATA['objects'] as $id => $v) 
						dataset('db_update', tb($_table), "$_order = ".$t++, "$_crit AND id=$id");
			}
			
			exit;	// ajax: no more processing needed
			break;
}
	
	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'sort')));
	exit;
}





switch (action(array('create_object', 'copy_objects', 'move_objects', 'kill_objects', 'export_objects', 'import_objects', 'import_price'))) {	// if POST contains submit button name

	case 'import_price':
			if ($f = $_FILES['price-file']) {
				$ext = parse_fname($f['name']);
				
				if (strtolower($ext['ext']) == 'pdf')
					move_uploaded_file($f['tmp_name'], $_SERVER['DOCUMENT_ROOT'].FP_UPLOAD_PRICE."$c-price.pdf");
			}
		break;

	case 'create_object':
		
		if ($title = trim(postdata('title'))) {

			$max = ($m = datafetch('db_item', tb($_table), $_crit, "MAX($_order) as m")) ? $m['m']+1 : 1;
				
			if ($id = datafetch('db_insert', tb($_table), array('id_parent' => $c, 
					'attr' => 0, 
					'title' => $title, 
					$_order => $max, 
			 	))) {

				@mkdir($_SERVER['DOCUMENT_ROOT']."$_path$id");
				@mkdir($_SERVER['DOCUMENT_ROOT']."$_path$id/$_tn");
				
				// redirect to edit page
				header("Location: "._transformURL($CURRENT->url, array('add_param' => "edit:$id")));
				exit;
			}
				// !!! else error on insert - handle it
		}
		
		break;

	case 'copy_objects':

		if (is_array($obj = postdata('sel')) && ($s = postdata('section'))) {
			
			if (sizeof($o = datafetch_array('db_list', array('table' => tb($_table), 'where' => "$_crit AND id in (".implode(',', $obj).")", 'order' => $_order, 'index' => 'id' )))) {

				$max = ($m = datafetch('db_item', tb($_table), "id_parent=$s", "MAX($_order) as m")) ? $m['m']+1 : 1;

				foreach ($o as $a => $v) {
	
					$dat = array('id_parent' => $s, $_order => $max++, 'attr' => $v['attr']);
					
					foreach ($_values as $val) $dat[$val] = $v[$val];

					if ($id = datafetch('db_insert', tb($_table), $dat)) {


						//copy materials selection
						
						if (is_array($temp = datafetch_array('db_list', array('table' => tb('data'), 'where' => "id_data=".D_MATERIALS." AND id_parent=$a", 'index' => 'id'))) 
							&& sizeof($temp)) {

							foreach($temp as $mate)
								dataset('db_insert', tb('data'), array('id_data' => D_MATERIALS, 'id_parent' => $id, 'name' => 'material', 'tag' => $mate['tag']));
						}
						
						
						//copy files
						
						if (is_file($_SERVER['DOCUMENT_ROOT']."$_path$a.jpg"))
							copy($_SERVER['DOCUMENT_ROOT']."$_path$a.jpg", $_SERVER['DOCUMENT_ROOT']."$_path$id.jpg");

						if (is_file($_SERVER['DOCUMENT_ROOT']."$_path$a-2.jpg"))
							copy($_SERVER['DOCUMENT_ROOT']."$_path$a-2.jpg", $_SERVER['DOCUMENT_ROOT']."$_path$id-2.jpg");

						if (is_file($_SERVER['DOCUMENT_ROOT']."$_path$a-small.jpg"))
							copy($_SERVER['DOCUMENT_ROOT']."$_path$a-small.jpg", $_SERVER['DOCUMENT_ROOT']."$_path$id-small.jpg");

						if (is_file($_SERVER['DOCUMENT_ROOT']."$_path$a-elements.jpg"))
							copy($_SERVER['DOCUMENT_ROOT']."$_path$a-elements.jpg", $_SERVER['DOCUMENT_ROOT']."$_path$id-elements.jpg");

						if (is_file($_SERVER['DOCUMENT_ROOT']."$_path$a-special.jpg"))
							copy($_SERVER['DOCUMENT_ROOT']."$_path$a-special.jpg", $_SERVER['DOCUMENT_ROOT']."$_path$id-special.jpg");

						if (is_file($_SERVER['DOCUMENT_ROOT']."$_path$a-price.pdf"))
							copy($_SERVER['DOCUMENT_ROOT']."$_path$a-price.pdf", $_SERVER['DOCUMENT_ROOT']."$_path$id-price.pdf");


						if (is_dir($_SERVER['DOCUMENT_ROOT']."$_path$a")) {
							copydir($_SERVER['DOCUMENT_ROOT']."$_path$a", $_SERVER['DOCUMENT_ROOT']."$_path$id");
							copydir($_SERVER['DOCUMENT_ROOT']."$_path$a/$_tn", $_SERVER['DOCUMENT_ROOT']."$_path$id/$_tn");
						}

						if (is_dir($_SERVER['DOCUMENT_ROOT']."$_path$a-2")) {
							copydir($_SERVER['DOCUMENT_ROOT']."$_path$a-2", $_SERVER['DOCUMENT_ROOT']."$_path$id-2");
							copydir($_SERVER['DOCUMENT_ROOT']."$_path$a-2/$_tn", $_SERVER['DOCUMENT_ROOT']."$_path$id-2/$_tn");
						}


					}
					
					
				}
				
				header("Location: .\n");
				exit;
			
			}
	
//			debug($obj);
//			debug($s);
		}
		
		break;

	case 'move_objects':

		if (is_array($obj = postdata('sel')) && ($s = (int)postdata('section'))) {

			$max = ($m = datafetch('db_item', tb($_table), "id_parent=$s", "MAX($_order) as m")) ? $m['m']+1 : 1;

			dataset('db_update', tb($_table), "id_parent=$s, $_order=$max", "$_crit AND id in (".implode(',', $obj).")");

			header("Location: .\n");
			exit;
		}
		
		break;

	case 'kill_objects':

		if (is_array($obj = postdata('sel'))) {
	
			dataset('db_delete', tb($_table), "$_crit AND id in (".implode(',', $obj).")");

			dataset('db_delete', tb('data'), "id_data in (".D_MATERIALS.",".D_MATERIAL_SETS.") AND id_parent in (".implode(',', $obj).")");

			foreach ($obj as $a) {

				@unlink($_SERVER['DOCUMENT_ROOT']."$_path$a.jpg");
				@unlink($_SERVER['DOCUMENT_ROOT']."$_path$a-2.jpg");
				@unlink($_SERVER['DOCUMENT_ROOT']."$_path$a-small.jpg");
				@unlink($_SERVER['DOCUMENT_ROOT']."$_path$a-elements.jpg");
				@unlink($_SERVER['DOCUMENT_ROOT']."$_path$a-special.jpg");
				@unlink($_SERVER['DOCUMENT_ROOT']."$_path$a-price.pdf");

				killdir($_SERVER['DOCUMENT_ROOT']."$_path$a/$_tn");
				killdir($_SERVER['DOCUMENT_ROOT']."$_path$a");

				killdir($_SERVER['DOCUMENT_ROOT']."$_path$a-2/$_tn");
				killdir($_SERVER['DOCUMENT_ROOT']."$_path$a-2");
			}

			header("Location: .\n");
			exit;
		}
		
		break;

	case 'export_objects':

		if (is_array($obj = postdata('sel'))) {
			
			if (sizeof($o = datafetch_array('db_list', array('table' => tb($_table), 'where' => "$_crit AND id in (".implode(',', $obj).")", 'order' => $_order, 'index' => 'id' )))) {

				$mats = datafetch_array('db_list', array('table' => tb('list'), 'where' => "id_list=".L_MATERIALS, 'index' => 'id'));
				$matsets = datafetch_array('db_list', array('table' => tb('list'), 'where' => "id_list=".L_MATERIAL_SETS, 'index' => 'id'));

				$logos = datafetch_array('db_list', array('table' => tb('list'), 'where' => "id_list=".L_LOGOS, 'index' => 'id'));

				$document = new DOMDocument('1.0', 'utf-8');
				$catNode = $document->createElement("cat");
				$catNode->setAttribute("type", $_cattype);
				$document->appendChild($catNode);
				
				$attach = array();

				foreach ( $o as $a => $v ) {

					$objNode = $document->createElement("object");
					$catNode->appendChild($objNode);
					
					$objNode->setAttribute("id", $v['id']);
					$objNode->setAttribute("attr", $v['attr']);
					
					foreach ($_values as $val)
						$objNode->setAttribute($val, $v[$val]);
						
					$objNode->setAttribute("brand", $logos[$v['id_brand']]['title']);
// materials
					if (sizeof($temp = datafetch_array('db_list', array('table' => tb('data'), 'where' => "id_data=".D_MATERIALS." AND id_parent=$a", 'index' => 'id')))) {
						foreach($temp as $mate) {
							
							$matNode = $document->createElement("material");
							$objNode->appendChild($matNode);
							
							$matNode->setAttribute("id", $mate['tag']);
							$matNode->setAttribute("title", $mats[$mate['tag']]['title']);
							$matNode->setAttribute("text", $mats[$mate['tag']]['text']);
						}
					}
// material sets
					if (sizeof($temp = datafetch_array('db_list', array('table' => tb('data'), 'where' => "id_data=".D_MATERIAL_SETS." AND id_parent=$a", 'index' => 'id')))) {
						
						foreach($temp as $mate) {
							
							$matNode = $document->createElement("matset");
							$objNode->appendChild($matNode);
							
							$matNode->setAttribute("id", $mate['tag']);
							$matNode->setAttribute("index", $mate['id_index']);
							$matNode->setAttribute("value", $mate['value']);
							$matNode->setAttribute("title", $matsets[$mate['tag']]['title']);
						}
					}

					if (is_file($_SERVER['DOCUMENT_ROOT']."$_path$a.jpg")) {
						$fNode = $document->createElement("file");
						$objNode->appendChild($fNode);
						
						$fNode->setAttribute("name", "<id>.jpg");
						$attach[] = array('archName' => "$a.jpg", 'fName' => $_SERVER['DOCUMENT_ROOT']."$_path$a.jpg");
					}

					if (is_file($_SERVER['DOCUMENT_ROOT']."$_path$a-2.jpg")) {
						$fNode = $document->createElement("file");
						$objNode->appendChild($fNode);
						
						$fNode->setAttribute("name", "<id>-2.jpg");
						$attach[] = array('archName' => "$a-2.jpg", 'fName' => $_SERVER['DOCUMENT_ROOT']."$_path$a-2.jpg");
					}

					if (is_file($_SERVER['DOCUMENT_ROOT']."$_path$a-small.jpg")) {
						$fNode = $document->createElement("file");
						$objNode->appendChild($fNode);
						
						$fNode->setAttribute("name", "<id>-small.jpg");
						$attach[] = array('archName' => "$a-small.jpg", 'fName' => $_SERVER['DOCUMENT_ROOT']."$_path$a-small.jpg");
					}

					if (is_file($_SERVER['DOCUMENT_ROOT']."$_path$a-elements.jpg")) {
						$fNode = $document->createElement("file");
						$objNode->appendChild($fNode);
						
						$fNode->setAttribute("name", "<id>-elements.jpg");
						$attach[] = array('archName' => "$a-elements.jpg", 'fName' => $_SERVER['DOCUMENT_ROOT']."$_path$a-elements.jpg");
					}

					if (is_file($_SERVER['DOCUMENT_ROOT']."$_path$a-special.jpg")) {
						$fNode = $document->createElement("file");
						$objNode->appendChild($fNode);
						
						$fNode->setAttribute("name", "<id>-special.jpg");
						$attach[] = array('archName' => "$a-special.jpg", 'fName' => $_SERVER['DOCUMENT_ROOT']."$_path$a-special.jpg");
					}

					if (is_file($_SERVER['DOCUMENT_ROOT']."$_path$a-price.pdf")) {
						$fNode = $document->createElement("file");
						$objNode->appendChild($fNode);
						
						$fNode->setAttribute("name", "<id>-price.pdf");
						$attach[] = array('archName' => "$a-price.pdf", 'fName' => $_SERVER['DOCUMENT_ROOT']."$_path$a-price.pdf");
					}


					if ($dir = @opendir($src = $_SERVER['DOCUMENT_ROOT']."$_path$a")) {
						while (($file = @readdir($dir))!==false)
							if (is_file("$src/$file")) {
								
								$fNode = $document->createElement("file");
								$objNode->appendChild($fNode);
								
								$fNode->setAttribute("name", "<id>/$file");
								$attach[] = array('archName' => "$a/$file", 'fName' => "$src/$file");
							}
						closedir($dir);
					}

					if ($dir = @opendir($src = $_SERVER['DOCUMENT_ROOT']."$_path$a/$_tn")) {
						while (($file = @readdir($dir))!==false)
							if (is_file("$src/$file")) {
								
								$fNode = $document->createElement("file");
								$objNode->appendChild($fNode);
								
								$fNode->setAttribute("name", "<id>/$_tn$file");
								$attach[] = array('archName' => "$a/$_tn$file", 'fName' => "$src/$file");
							}
						closedir($dir);
					}

					if ($dir = @opendir($src = $_SERVER['DOCUMENT_ROOT']."$_path$a-2")) {
						while (($file = @readdir($dir))!==false)
							if (is_file("$src/$file")) {
								
								$fNode = $document->createElement("file");
								$objNode->appendChild($fNode);
								
								$fNode->setAttribute("name", "<id>-2/$file");
								$attach[] = array('archName' => "$a-2/$file", 'fName' => "$src/$file");
							}
						closedir($dir);
					}

					if ($dir = @opendir($src = $_SERVER['DOCUMENT_ROOT']."$_path$a-2/$_tn")) {
						while (($file = @readdir($dir))!==false)
							if (is_file("$src/$file")) {
								
								$fNode = $document->createElement("file");
								$objNode->appendChild($fNode);
								
								$fNode->setAttribute("name", "<id>-2/$_tn$file");
								$attach[] = array('archName' => "$a-2/$_tn$file", 'fName' => "$src/$file");
							}
						closedir($dir);
					}
					
				}
			
				$zipfile = new Ziplib;
				$zipfile->zl_add_file($document->saveXML(), "data.xml", "g9");
				
				foreach($attach as $att) {
					$attf = fopen($att['fName'], "rb");
					$contents = fread($attf, filesize($att['fName']));
					fclose($attf);
					
					$zipfile->zl_add_file($contents, $att['archName'], "g1");
				}

				header("Content-type: application/zip");
				header("Content-Disposition: attachment; filename=\"data.zip\"");
				echo $zipfile->zl_pack('');
			
				exit;
			}
		}
		break;


	case 'import_objects':

		$_replace_existing_data = (boolean)postdata('replace_data');											// !!!need to be set via interface


		if (($f = $_FILES['upload']) && $f['tmp_name']) {
			
			if ($zip = @zip_open($f['tmp_name'])) {
				
				if ((@zip_entry_name($d = @zip_read($zip)) == 'data.xml') && (@zip_entry_open($zip, $d, "r"))) {
					
					if (($xml = DOMDocument::loadXML(@zip_entry_read($d, @zip_entry_filesize($d)))) && ($cat = $xml->getElementsByTagName('cat')) && $cat->length && ($cat = $cat->item(0))) {
						
						if ($cat->getAttribute('type') == $_cattype) {

							if (($o = $cat->getElementsByTagName('object')) && $o->length) {

								$max = ($m = datafetch('db_item', tb($_table), $_crit, "MAX($_order) as m")) ? $m['m']+1 : 1;
								$mats = datafetch_array('db_list', array('table' => tb('list'), 'where' => "id_list=".L_MATERIALS, 'index' => 'id'));
								
								$matsets = datafetch_array('db_list', array('table' => tb('list'), 'where' => "id_list=".L_MATERIAL_SETS, 'index' => 'title'));	// choose matset by title

								$logos = datafetch_array('db_list', array('table' => tb('list'), 'where' => "id_list=".L_LOGOS, 'index' => 'id'));
								
								foreach($o as $v) {
		
									$id_old = $v->getAttribute('id');
			
									$dat = array('id_parent' => $c, $_order => $max++, 'attr' => $v->getAttribute('attr'));
									foreach ($_values as $val) $dat[$val] = $v->getAttribute($val);
									
									$brand = $v->getAttribute('brand');
									
									if ($id_brand = $v->getAttribute('id_brand')) {
										if (!isset($logos[$id_brand]) || ($brand && ($brand != $logos[$id_brand]['title']))) {
											
											$new_id_brand = 0;
											
											foreach($logos as $l => $logo) 
												if ($logo['title'] == $brand)
													$new_id_brand = $l;
											
											$dat['id_brand'] = $new_id_brand ? $new_id_brand : datafetch('db_insert', tb('list'), array('id_list' => L_LOGOS, 'title' => $brand));
										}
									}
		
									$id = datafetch('db_insert', tb($_table), $dat);
		
									if (($temp = $v->getElementsByTagName('material')) && $temp->length) {
		
										foreach($temp as $mate) {
											
											$m_id = $mate->getAttribute('id');
											$m_title = $mate->getAttribute('title');
											$m_text = $mate->getAttribute('text');
											
											if (!isset($mats[$m_id]) || ($m_title && ($m_title != $mats[$m_id]['title']))) {
												
												$new_m_id = 0;
												
												foreach($mats as $m => $mat) 
													if ($mat['title'] == $m_title)
														$new_m_id = $m;
												
												$m_id = $new_m_id ? $new_m_id : datafetch('db_insert', tb('list'), array('id_list' => L_MATERIALS, 'title' => $m_title, 'text' => $m_text));
											}
											
											dataset('db_insert', tb('data'), array('id_data' => D_MATERIALS, 'id_parent' => $id, 'name' => 'material', 'tag' => $m_id));
										}
									}



									if (($temp = $v->getElementsByTagName('matset')) && $temp->length) {
		
										foreach($temp as $mate) {
											
											$m_id = $mate->getAttribute('id');
											$m_title = $mate->getAttribute('title');
											$m_value = $mate->getAttribute('value');
											$m_index = $mate->getAttribute('index');
											
											if (isset($matsets[$m_title])) {
												
												$m_id = $matsets[$m_title]['id'];
						
											}
											
											dataset('db_insert', tb('data'), array('id_data' => D_MATERIAL_SETS, 'id_parent' => $id, 'id_index' => $m_index, 'name' => 'set', 'value' => $m_value, 'tag' => $m_id));
										}
									}


									
									if (($temp = $v->getElementsByTagName('file')) && $temp->length) {
										
										foreach($temp as $file) {
											
											$ft = $file->getAttribute('name');
											$oldname = str_replace('<id>', $id_old, $ft);
											$newname = array_mark_bounds(split('/', str_replace('<id>', $id, $ft)));
										
											if (($f = @zip_read($zip)) && (@zip_entry_name($f) == $oldname) && @zip_entry_open($zip, $f, "r")) {
												$buf = @zip_entry_read($f, @zip_entry_filesize($f));
												@zip_entry_close($f);
												
												$fn = array_pop($newname);
												
												$p = '';
												if (sizeof($newname)) {
													foreach($newname as $dir) {
														if (!is_dir($_SERVER['DOCUMENT_ROOT'].$_path.($p .= $dir)))
															@mkdir($_SERVER['DOCUMENT_ROOT'].$_path.$p);
														$p .= '/';
													}
												}
												
	//	debug($xxx = array('old' => $oldname, 'new p' => $p, 'new f' => $fn));
		
												$h = fopen($_SERVER['DOCUMENT_ROOT'].$_path.$p.$fn, "wb");
												fwrite($h, $buf);
												fclose($h);
												
											} else {
												diags("Беда с файлами в архиве");
												break;
											}
										
										}
										
									}
									
								}
						 
							} else diags("Файл описания пустой.");
	
						} else diags("Не тот архив.");
	
					} else diags("Поврежден файл описания.");
	
				} else diags("Это не наш архив.");
			
				@zip_close($zip);
				
			} else diags("Это был вообще не архив...");

		} else diags("Сначала выберите файл, потом — загружайте.");


//		debug($DIAGS);

		$DATA['objects'] = array_mark_bounds( datafetch_array('db_list', array('table' => tb($_table), 'where' => $_crit, 'order' => $_order, 'index' => 'id' )));

		break;
}

$DATA['price-file'] = is_file($_SERVER['DOCUMENT_ROOT'].FP_UPLOAD_PRICE."$c-price.pdf");

?>